package rd.hardcore.ui.header 
{
	import aze.motion.eaze;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.navigateToURL;
	import flash.net.URLRequest;
	import flash.ui.Mouse;
	import flash.ui.MouseCursor;
	import fr.digitas.flowearth.conf.Conf;
	import rd.hardcore.events.ResizeEvent;
	import rd.hardcore.params.GlobalEvents;
	import rd.hardcore.params.GlobalParams;
	import rd.hardcore.ui.controls.Controls;
	import rd.hardcore.ui.header.buttons.HeaderButton;
	import rd.hardcore.ui.header.buttons.HeaderButton_CLOSE;
	import rd.hardcore.ui.header.buttons.HeaderButton_CTRLS;
	import rd.hardcore.ui.header.buttons.HeaderButton_HELP;
	import rd.hardcore.ui.header.buttons.HeaderButton_REDUCE;
	/**
	 * Application's Header
	 * @author Romain Prache
	 */
	public class Header extends Header_FC
	{
		
		private var bt_reduce	: HeaderButton;
		private var bt_close	: HeaderButton;
		private var bt_ctrls	: HeaderButton;
		private var bt_help		: HeaderButton;
		
		private var helpUrl		: String;
		private var helpTgt		: String;
		
		/* Header */
		public function Header() 
		{
			bt_help	= new HeaderButton_HELP();
			//addChild(bt_help);
			
			bt_help.x = 649;
			bt_help.y = 15;
			
			bt_ctrls = new HeaderButton_CTRLS();
			addChild(bt_ctrls);
			
			bt_ctrls.x = 676;
			bt_ctrls.y = 15;
			
			bt_reduce = new HeaderButton_REDUCE();
			addChild(bt_reduce);
			
			bt_reduce.x = 703;
			bt_reduce.y = 15;
			
			bt_close = new HeaderButton_CLOSE();
			addChild(bt_close);
			
			bt_close.x = 730;
			bt_close.y = 15;
			
			bt_reduce.addEventListener(MouseEvent.CLICK, 	handleMinimize);			
			bt_close.addEventListener(MouseEvent.CLICK, 	handleClose);
			bt_ctrls.addEventListener(MouseEvent.CLICK, 	handleControls);
			//bt_help.addEventListener(MouseEvent.CLICK, 		gotoHelpPage);
			
			bg.addEventListener(MouseEvent.MOUSE_DOWN, 	handleReadyToDrag);
			bg.addEventListener(MouseEvent.MOUSE_UP, 	handleLeaveDrag);
			bg.addEventListener(MouseEvent.MOUSE_OVER, 	dragOver);
			bg.addEventListener(MouseEvent.MOUSE_OUT, 	dragOut);
			
			alpha = 0;
			y = 20;
			x = (GlobalParams.WIDTH - width ) >> 1;
		}
		
		/* Actions */
		private function handleMinimize(e:MouseEvent):void 
		{
			dispatchEvent(new ResizeEvent(ResizeEvent.PLEASE_MINIMIZE, null, true));
		}
		
		private function handleClose(e:MouseEvent):void 
		{
			dispatchEvent(new Event(Event.CLOSE, true));
		}
		
		private function handleControls(e:MouseEvent) : void
		{
			dispatchEvent( new Event(Controls.OPEN_CLOSE, true));
		}
		
		
		private function gotoHelpPage(e:MouseEvent):void 
		{
			var xml : XML = Conf.getDatas("helpURL");
			
			helpUrl = Conf.getString("helpURL");
			helpTgt = xml.@target;
			
			navigateToURL( new URLRequest(helpUrl), helpTgt); 
		}

		
		
		/* Drag */
		private function dragOver(e:MouseEvent):void 
		{
			Mouse.cursor = MouseCursor.HAND;
		}
		
		private function dragOut(e:MouseEvent):void 
		{
			Mouse.cursor = MouseCursor.AUTO;
		}
		
		private function handleLeaveDrag(e:MouseEvent):void 
		{
			Mouse.cursor = MouseCursor.AUTO;
			dispatchEvent( new Event(GlobalEvents.STOP_DRAG_APP, true));
		}
		
		private function handleReadyToDrag(e:MouseEvent):void 
		{
			Mouse.cursor = MouseCursor.HAND;
			dispatchEvent( new Event(GlobalEvents.START_DRAG_APP, true));
		}
		
	}

}